package com.frame.pageform.component.validata;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;

import org.apache.commons.lang.StringUtils;


public class ValidateNumberGenerator {

	private int size=4;
	//0为纯数字，1为数字和字母混合，2为纯字母
	private int type;
	private String bgColor;
	private String fontColor;

	private ValidateNumberDisturber disturber=new ValidateNumberDisturber();
	public ValidateNumberGenerator(){

	}
	public ValidateNumberGenerator(int size,int type){
		this.size=size;
		this.type=type;
	}
	public ValidateNumberGenerator(int size,int type,String bgColor,String fontColor){
		this.size=size;
		this.type=type;
		this.fontColor=fontColor;
		this.bgColor=bgColor;
	}
	public String generateNumber(){
		Random random = new Random();
		String s = "";
		if(type==1){
			//数字与字母
			char c;
			for(int i=0;i<size;i++){
				int n = random.nextInt(36);

				if(n < 10)
					c = (char)('0' + n);
				else
					c = (char)('A' + (n - 10));

				if(c == 'O' || c == '0')
					c = 'A';
				s+=c;
			}

		}else if(type==2){
			//纯字母(大小写混合)
			char[] str={'A','a'};
			for(int i=0;i<size;i++){
				char c;
				char m=str[random.nextInt(2)];
				int n = random.nextInt(26);
				c = (char)(m + n);
				s+=c;
			}
		}else{
			for(int i=0;i<size;i++){
				int n=random.nextInt(10);
				s+=n;
			}
		}
		return s;
	}
	public BufferedImage generateImage(String number)
	{
		int width=13;
		int imgheight = (int)(width * 1.6);
		int n = number.length();
		int imgwidth=width*n;

		BufferedImage image = new BufferedImage(imgwidth,imgheight,
				BufferedImage.TYPE_INT_RGB);

		Graphics2D g = image.createGraphics();
		Color background = Color.lightGray;
		if(StringUtils.isNotBlank(bgColor)){
			try{
				String[] colors=bgColor.split(",");
				background=new Color(Integer.parseInt(colors[0]),Integer.parseInt(colors[1]),Integer.parseInt(colors[2]));
			}catch(Exception e){}
		}
		g.setColor(background);
		g.fillRect(0,0,width * n,imgheight);

		Random random = new Random();
		Color color = Color.blue;
		if(StringUtils.isNotBlank(fontColor)){
			try{
				String[] colors=fontColor.split(",");
				color=new Color(Integer.parseInt(colors[0]),Integer.parseInt(colors[1]),Integer.parseInt(colors[2]));
			}catch(Exception e){}
		}
		String fontName = "Times New Roman";

		Font font = new Font(fontName,0,(int)(width * 1.4));

		/**干扰线**/
		disturber.disturber(g,imgwidth,imgheight);

		g.setStroke(new BasicStroke(width / 5));
		g.setColor(color);
		g.setFont(font);
		g.translate( -width / 2,imgheight / 2);

		for(int i = 0; i < n; i++)
		{
			//竖直偏移
			int l = (int)(width * 0.3);
			int yOffset = random.nextInt(l * 2) - l;

			//旋转角度
			int angle = random.nextInt(50) - 25;
			double a = angle * Math.PI / 180;

			g.translate(width,0);
			g.rotate(a);
			g.drawString(String.valueOf(number.charAt(i)), -(int)(width * 0.5),
					(int)(width * 0.5) + yOffset);

			//转回正常角度
			g.rotate( -a);
		}

		g.dispose();
		return image;
	}
}
